package com.example.dllo.keepdemo.train.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.base.BaseActivity;
import com.example.dllo.keepdemo.dynamic.DynamicDetailActivity;
import com.example.dllo.keepdemo.tools.GsonRequest;
import com.example.dllo.keepdemo.tools.VolleySingleton;
import com.example.dllo.keepdemo.train.adapter.TrainDynamicListViewAdapter;
import com.example.dllo.keepdemo.train.bean.TrainDynamicBean;

/**
 * Created by dllo on 16/1/16.
 */
public class TrainDynamicActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private ListView listView;
    private TrainDynamicListViewAdapter adapter;
    private Toolbar toolbar;
    private TrainDynamicBean trainDynamicBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traindynamic);
        listView = (ListView) findViewById(R.id.listview_activity_traindynamic);
        listView.setDivider(null);
        listView.setOnItemClickListener(this);
        toolbar = (Toolbar) findViewById(R.id.toolbar_activity_traindynamic);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.actionlastbtn);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TrainDynamicActivity.this.finish();
            }
        });

        initData();
    }

    private void initData() {
        trainDynamicBean = new TrainDynamicBean();
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String url = "https://api.gotokeep.com/v1.1/workouts/" + id + "/timeline";
        GsonRequest<TrainDynamicBean> gsonRequest = new GsonRequest<TrainDynamicBean>(url, TrainDynamicBean.class, new Response.Listener<TrainDynamicBean>() {
            @Override
            public void onResponse(TrainDynamicBean response) {
                trainDynamicBean = response;
                adapter = new TrainDynamicListViewAdapter(TrainDynamicActivity.this,response);
                listView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(gsonRequest);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, position + "跳到动态详情", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, DynamicDetailActivity.class);
        intent.putExtra("id",trainDynamicBean.getData().get(position).get_id());
        startActivity(intent);
    }
}
